package com.gexiaobao.pigeon.ui.auction.fragment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentFoucsOnAuctionSubjectBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterFollowSubject
import com.gexiaobao.pigeon.ui.auction.ActivityThematicList
import com.gexiaobao.pigeon.viewmodel.AuctionViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/11/13 13:27
 * @Describe    : 关注的主题
 */
class FragmentFocusOnAuctionSubject : BaseFragment<AuctionViewModel, FragmentFoucsOnAuctionSubjectBinding>() {

    private lateinit var loadsir: LoadService<Any>
    private val mAdapter: AdapterFollowSubject by lazy { AdapterFollowSubject(arrayListOf()) }
    private var mFollowType = 0

    override fun initView(savedInstanceState: Bundle?) {
        mFollowType = requireArguments().getInt("followType")
        /** 状态页配置 */
        loadsir = loadServiceInit(mDatabind.includeList.swipeRefreshLayout) {
            /** 点击重试时触发的操作 */
            onRefresh(isRefresh = true)
        }
        onRefresh(isRefresh = true)

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 12f)))
            it.initFooter {
                onRefresh(isRefresh = false)
            }
        }

        mDatabind.includeList.swipeRefreshLayout.init {
            onRefresh(isRefresh = true)
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            startActivity<ActivityThematicList>("auctionId" to mAdapter.data[position].auctionId.toString())
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.followAuctionsResult.observe(viewLifecycleOwner) {
            loadListData(it, mAdapter, loadsir, mDatabind.includeList.swipeRecyclerView, mDatabind.includeList.swipeRefreshLayout)
        }
    }

    private fun onRefresh(isRefresh: Boolean) {
        loadsir.showLoading()
        mViewModel.getFollowAndJoinAuctionList(isRefresh, mFollowType)
    }

    companion object {
        fun newInstance(followType: Int): FragmentFocusOnAuctionSubject {
            val fragment = FragmentFocusOnAuctionSubject()
            val bundle = Bundle()
            fragment.arguments = bundle
            bundle.putInt("followType", followType)
            return fragment
        }
    }
}